﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace _08_io
{
    class Program
    {
        static void Main(string[] args)
        {
            TestFile();
        }

        /* ------ 1. 测试 File 类 ------ */
        public static void TestFile()
        {
            // 1. 读取所有的字节返回一个字节数组
            //byte[] buff = File.ReadAllBytes(@"E:\\test.txt");
            //string res = Encoding.Default.GetString(buff);
            //Console.WriteLine(res);
            // 1.2 写入字节数组到指定路径
            //File.WriteAllBytes(@"E:\\test1.txt",buff);


            //2.读取所有的行,指定编码格式为默认aisn
            string[] lines = File.ReadAllLines(@"E:\\test.txt", Encoding.Default);
            Console.WriteLine(lines[0]);

            // 3.读取整个文档的文本内容
            string content = File.ReadAllText(@"E:\\test.txt", Encoding.Default);

            Console.WriteLine(content);
            Console.ReadKey();
        }

        /* ------- 2. FileStream ------- */
        public static void TestFileStream()
        {
            //文件路径,打开方式,文件操作方式
            FileStream fsRead = new FileStream(@"E:\\test.txt", FileMode.OpenOrCreate, FileAccess.Read);
            FileStream fsWrite = new FileStream(@"E:\\test3.txt", FileMode.OpenOrCreate, FileAccess.Write);
            byte[] buff = new byte[1024];
            int len = 0;
            while ((len = fsRead.Read(buff, 0, buff.Length)) != 0)
            {
                string str = Encoding.Default.GetString(buff, 0, len);
                Console.WriteLine(str);
                fsWrite.Write(buff, 0, len);
            }
            Console.WriteLine("OK");


            //关闭流
            fsRead.Close();
            fsWrite.Close();
            //释放资源
            fsRead.Dispose();
            fsWrite.Dispose();

            Console.ReadKey();
        }

        //测试自定义的方法
        public static void TestCopyFile(string[] args)
        {
            CopyFile(@"E:\\test3.txt", @"E:\\text4.txt");
        }

        //使用 FileStream 封装复制文件的方法
        public static void CopyFile(string source, string target)
        {
            //使用 using 来管理文件流的关闭和释放
            using (FileStream fsRead = new FileStream(source, FileMode.OpenOrCreate, FileAccess.Read))
            {
                using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    byte[] buff = new byte[1024];
                    int len = 0;
                    while ((len = fsRead.Read(buff, 0, buff.Length)) != 0)
                    {
                        fsWrite.Write(buff, 0, len);
                    }
                    Console.WriteLine("OK");
                }
            }

        }// end of copyFile()

        /* -------- 3. StreamReader 字符读写------------ */
        public static void TestStreamReader()
        {
            using (StreamReader sr = new StreamReader(@"E:\\text4.txt", Encoding.Default))
            {
                using (StreamWriter sw = new StreamWriter(@"E:\\test5.txt", true))
                {
                    //如果没到流的结尾
                    while (!sr.EndOfStream)
                    {
                        string str = sr.ReadLine();
                        Console.WriteLine(str);
                        sw.WriteLine(str);
                    }
                }
            }
            Console.WriteLine("OK");
            Console.ReadKey();

        }


    }

}
